package cn.edu.nju.ws.sview.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashSet;
import java.util.Properties;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.SAXException;

import cn.edu.nju.ws.sview.cache.URIIDCache;
import cn.edu.nju.ws.sview.views.View;

/**
 * get the property list in the xml file
 * @author ssgong
 * @author Qingxia Liu
 */
public class PropertyList {
	private final static  HashSet<String> directURIProperties;
	private final static HashSet<String> directLabelProperties;
	
	 static{
		 directURIProperties = PropertyList.initDirectURIProperties();
		 directLabelProperties = PropertyList.initDirectLabelProperties();
	  }
	
	/*
	 * get label properties in the xml 
	 */
	public static ArrayList<String> getLabelProperties(){
		ArrayList<String> result = new ArrayList<String>();
		InputStream stream = PropertyList.class.getClassLoader().getResourceAsStream("labelp.xml");
	    PropertyHandler phandle = new PropertyHandler();
	    SAXParserFactory saxparserfactory = SAXParserFactory.newInstance();
	    try {
			SAXParser saxparser = saxparserfactory.newSAXParser();
			saxparser.parse(stream, phandle) ;
			result.addAll(phandle.getURIs()) ;
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}

	public static ArrayList<String> getImgProperties(){
		ArrayList<String> result = new ArrayList<String>();
		InputStream stream = PropertyList.class.getClassLoader().getResourceAsStream("imgp.xml");
	    PropertyHandler phandle = new PropertyHandler();
	    SAXParserFactory saxparserfactory = SAXParserFactory.newInstance();
	    try {
			SAXParser saxparser = saxparserfactory.newSAXParser();
			saxparser.parse(stream, phandle) ;
			result.addAll(phandle.getURIs()) ;
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	
	public static ArrayList<String> getCommentProperties(){
		ArrayList<String> result = new ArrayList<String>();
		InputStream stream = PropertyList.class.getClassLoader().getResourceAsStream("commentp.xml");
	    PropertyHandler phandle = new PropertyHandler();
	    SAXParserFactory saxparserfactory = SAXParserFactory.newInstance();
	    try {
			SAXParser saxparser = saxparserfactory.newSAXParser();
			saxparser.parse(stream, phandle) ;
			result.addAll(phandle.getURIs()) ;
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	/**
	 * by lqx for dataset
	 * @return
	 */
	public static ArrayList<String> getDescriptionProperties(){
		ArrayList<String> result = new ArrayList<String>();
		InputStream stream = PropertyList.class.getClassLoader().getResourceAsStream("descriptionp.xml");
	    PropertyHandler phandle = new PropertyHandler();
	    SAXParserFactory saxparserfactory = SAXParserFactory.newInstance();
	    try {
			SAXParser saxparser = saxparserfactory.newSAXParser();
			saxparser.parse(stream, phandle) ;
			result.addAll(phandle.getURIs()) ;
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	/**
	 * by lqx for dataset
	 * @return
	 */
	public static ArrayList<String> getContributorProperties(){
		ArrayList<String> result = new ArrayList<String>();
		InputStream stream = PropertyList.class.getClassLoader().getResourceAsStream("contributorp.xml");
	    PropertyHandler phandle = new PropertyHandler();
	    SAXParserFactory saxparserfactory = SAXParserFactory.newInstance();
	    try {
			SAXParser saxparser = saxparserfactory.newSAXParser();
			saxparser.parse(stream, phandle) ;
			result.addAll(phandle.getURIs()) ;
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	/**
	 * by lqx for dataset
	 * @return
	 */
	public static ArrayList<String> getDateProperties(){
		ArrayList<String> result = new ArrayList<String>();
		InputStream stream = PropertyList.class.getClassLoader().getResourceAsStream("datep.xml");
	    PropertyHandler phandle = new PropertyHandler();
	    SAXParserFactory saxparserfactory = SAXParserFactory.newInstance();
	    try {
			SAXParser saxparser = saxparserfactory.newSAXParser();
			saxparser.parse(stream, phandle) ;
			result.addAll(phandle.getURIs()) ;
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	public static ArrayList<String> getNameForIndexProperties(){
		ArrayList<String> result = new ArrayList<String>();
		InputStream stream = PropertyList.class.getClassLoader().getResourceAsStream("namep.xml");
	    PropertyHandler phandle = new PropertyHandler();
	    SAXParserFactory saxparserfactory = SAXParserFactory.newInstance();
	    try {
			SAXParser saxparser = saxparserfactory.newSAXParser();
			saxparser.parse(stream, phandle);
			result.addAll(phandle.getURIs());
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
//	/**
//	 * by lqx for dataset
//	 * @return
//	 */
//	public static ArrayList<String> getTopicProperties(){
//		ArrayList<String> result = new ArrayList<String>();
//		InputStream stream = PropertyList.class.getClassLoader().getResourceAsStream("topicp.xml");
//		PropertyHandler phandle = new PropertyHandler();
//		SAXParserFactory saxparserFactory = SAXParserFactory.newInstance();
//		try {
//			SAXParser saxparser = saxparserFactory.newSAXParser();
//			saxparser.parse(stream, phandle);
//			result.addAll(phandle.getURIs());
//		} catch (ParserConfigurationException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (SAXException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		return result;
//	}
	public static HashSet<Integer> getDirectURIProperties(){
		HashSet<Integer> set = new HashSet<Integer>();
		for(String str:directURIProperties){
			int id;
			try {
				id = URIIDCache.getInstance().getURIID(str, true);
				set.add(id);
			} catch (Throwable e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return set;
	}
	
	public static HashSet<Integer> getDirectLabelProperties(){
		HashSet<Integer> set = new HashSet<Integer>();
		for(String str:directLabelProperties){
			int id;
			try {
				id = URIIDCache.getInstance().getURIID(str, true);
				set.add(id);
			} catch (Throwable e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return set;
	}
	
	private static HashSet<String> initDirectURIProperties(){
		HashSet<String> result = new HashSet<String>();
		InputStream stream = PropertyList.class.getClassLoader().getResourceAsStream("directurip.xml");
	    PropertyHandler phandle = new PropertyHandler();
	    SAXParserFactory saxparserfactory = SAXParserFactory.newInstance();
	    try {
			SAXParser saxparser = saxparserfactory.newSAXParser();
			saxparser.parse(stream, phandle) ;
			result.addAll(phandle.getURIs()) ;
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	
	private  static HashSet<String> initDirectLabelProperties(){
		HashSet<String> result = new HashSet<String>();
		InputStream stream = PropertyList.class.getClassLoader().getResourceAsStream("directlabelp.xml");
	    PropertyHandler phandle = new PropertyHandler();
	    SAXParserFactory saxparserfactory = SAXParserFactory.newInstance();
	    try {
			SAXParser saxparser = saxparserfactory.newSAXParser();
			saxparser.parse(stream, phandle) ;
			result.addAll(phandle.getURIs()) ;
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
}
